package com.want.evenchat.controller.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hyphenate.chat.EMGroup;
import com.want.evenchat.R;
import com.want.evenchat.model.Model;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Want on 2018/5/8.
 */

public class GroupListViewAdapter extends BaseAdapter{

    private Context mContext;

    private List<EMGroup> mGroups = new ArrayList<>();

    public GroupListViewAdapter(Context context) {
        mContext = context;
    }

    //刷新方法
    public void refresh(List<EMGroup> groups){
        if (groups != null && groups.size() >= 0){
            mGroups.clear();

            mGroups.addAll(groups);

            //刷新页面
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return mGroups == null ? 0:mGroups.size();
    }

    @Override
    public Object getItem(int position) {
        return mGroups.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        //创建或获取Viewholder
        ViewHolder holder = null;
        if (convertView == null){

            holder = new ViewHolder();

            convertView = View.inflate(mContext , R.layout.item_grouplist,null);

            holder.textview_name = convertView.findViewById(R.id.textview_grouplist_name);
        
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        //获取当前item数据
        EMGroup emGroup = mGroups.get(position);

        //展示数据
        holder.textview_name.setText(emGroup.getGroupName());

        //返回数据
        return convertView;
    }

    private class ViewHolder{
        TextView textview_name;
    }
}
